package com.hziee.mail.dao;

import com.hziee.mail.model.GoodsDirectionaryEntity;
import com.hziee.mail.model.OrderInfoTemporaryEntity;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author ZX
 */
public interface OrderTemporaryRepository extends JpaRepository<OrderInfoTemporaryEntity, Integer>, CrudRepository<OrderInfoTemporaryEntity, Integer> {

    /**
     * 查找直接购买商品
     *
     * @param goodsId
     * @param userId
     * @return
     */
    @Query(nativeQuery = true, value = "SELECT " +
            "goods_directionary.id, " +
            "goods_directionary.goods_id, " +
            "goods_directionary.goods_img, " +
            "order_info_temporary.value, " +
            "order_info_temporary.price, " +
            "product_class_info.product_class_name " +
            "FROM " +
            "order_info_temporary " +
            "JOIN goods_directionary on goods_directionary.goods_id = order_info_temporary.goods_id " +
            "JOIN product_class_info on product_class_info.product_class_id = :goodsId " +
            "WHERE order_info_temporary.goods_id = :goodsId AND order_info_temporary.user_id = :userId ")
    List<Map> getOrderTemporary(@Param("goodsId") String goodsId, @Param("userId") Integer userId);

    /**
     * 插入临时订单
     *
     * @param goodsId
     * @param value
     * @param price
     * @param userId
     */
    @Modifying
    @Transactional
    @Query(nativeQuery = true, value = "INSERT INTO order_info_temporary(user_id, goods_id, price, value) VALUES (:userId, :goodsId, :price, :value)")
    void insertOrderTemporary(@Param("goodsId") String goodsId, @Param("value") Integer value, @Param("price") double price, @Param("userId") Integer userId);
}
